﻿using MediatR;
using Newtonsoft.Json;
using WorkflowCore.Models;
using Yuekao.API.Write.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Write.CommandHander
{
    public class FormDataAddCommandHandler : IRequestHandler<FormDataAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<WorkflowDefinitionModel> baseRepository;
        public FormDataAddCommandHandler(IBaseRepository<WorkflowDefinitionModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        public Task<APIResult<int>> Handle(FormDataAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var list= baseRepository.GetValue(x=>x.DefinitionId==request.DefinitionId&&x.Version==request.Version);
            list.FormData = JsonConvert.SerializeObject(request.Formdata);
            baseRepository.Update(list);
            result.Code = APIEnum.Success;
            result.Message = "表单添加成功";
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
